<template>
    <div class="container">
        <div class="title">
            <span>修改</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="140px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="省份" prop="province_id">
                <el-select v-model="province_id" filterable placeholder="请选择">
                    <el-option
                            v-for="item in provinceArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="城市" prop="city_id">
                <el-select v-model="form.city_id" filterable placeholder="请选择">
                    <el-option
                            v-for="item in cityArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="区/县名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写区/县名称"></el-input>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm('form')">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import province from '@/models/province'
import city from '@/models/city'
import county from '@/models/county'

export default {
  props: {
    countyId: {
      type: Number,
    },
  },
  data() {
    return {
      provinceArr: [],
      cityArr: [],
      province_id: '',
      form: {
        name: '',
        province_id: '',
        city_id: '',
      },
    }
  },
  watch: {
    province_id(news_value, old_value) {
      this.form.province_id = news_value
      if (news_value !== old_value) {
        if (old_value !== '') {
          this.form.city_id = ''
        }
        this.getCityAll()
      }
    },
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    // 获取全部省份信息
    async getProvinceAll() {
      this.provinceArr = await province.getProvinceAll()
      this.$nextTick(() => {
        this.getCityAll()
      })
    },
    // 获取城市信息
    async getCityAll() {
      this.cityArr = await city.getCityAll({ province_id: this.form.province_id })
    },
    // 获取详情
    async getInfo() {
      const info = await county.getCountyInfo(this.countyId)
      this.form = {
        name: info.name,
        province_id: info.province_id,
        city_id: info.city_id,
      }
      this.province_id = info.province_id
      this.$nextTick(() => {
        this.getProvinceAll()
      })
    },
    // eslint-disable-next-line no-unused-vars
    async submitForm(formName) {
      const self = this
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: this.countyId,
        name: self.form.name,
        province_id: self.form.province_id,
        city_id: self.form.city_id,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await county.updateCounty(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.getInfo()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
